
function successcallback(msg, url)
{
    layer.msg(msg, {
        title: '信息',
        icon: 1,
        btn: false,
        time: 1000,
        closeBtn: 0
    }, function () {
        if (url == '')
        {
            window.location.reload();
        } else
        {
            window.location.href = url;
        }

    });
}

function submit() {
    var errorFun = arguments[0];
    var successFun = arguments[1];
    $('.formsub').ajaxSubmit(
            {
                //url: url,                 //默认是form的action
                //type: type,               //默认是form的method（get or post）
                dataType: "json", //html(默认), xml, script, json...接受服务端返回的类型
                //clearForm: true,          //成功提交后，清除所有表单元素的值
                //resetForm: true,          //成功提交后，重置所有表单元素的值
                //timeout: 3000,               //限制请求的时间，当请求大于3秒后，跳出请求
                //提交前的回调函数

                //提交成功后的回调函数
                success: function (data, status, xhr, $form) {
                    if (data.error != 0)//错误
                    {
                        if (errorFun)
                        {
                            errorFun();
                        }
                        // layer.msg(data.message, function () {});
                        layer.alert(data.message, {
                            icon: 2,
                            btn: false,
                            closeBtn: 0,
                            time: 2000
                        });

                    } else
                    {
                        if (successFun)
                        {
                            successFun();
                        }
                        if ($('.formsub').attr('url'))
                        {
                            window.location.href = $('.formsub').attr('url');
                        }

                    }

                },
                error: function (data, status, xhr, $form)
                {

                }

            }
    );

}




var ajaxform = {
    formobj: '.formsub',
    //成功提示
    oktipsoption: {
        type: 1, //跳转方式 0 直接跳转 1 提示后跳转 -1不跳转 - 2刷新 
        url: '',
        msg: '操作成功'
    },
    //错误之前执行函数
    errorbefore: function () {

    },

    init: function (oktipsoption, formelem) {
        if (formelem)
        {
            ajaxform.formobj = formelem;
        }
        if (typeof (oktipsoption.type) != "undefined") {
            ajaxform.oktipsoption.type = oktipsoption.type;
        }

        if (typeof (oktipsoption.url) != "undefined") {
            ajaxform.oktipsoption.url = oktipsoption.url;
        }
        if (typeof (oktipsoption.msg) != "undefined") {
            ajaxform.oktipsoption.msg = oktipsoption.msg;
        }



    },
    submit: function () {
        $(ajaxform.formobj).ajaxSubmit(
                {
                    dataType: "json", //html(默认), xml, script, json...接受服务端返回的类型
                    //提交成功后的回调函数
                    success: function (data, status, xhr, $form) {
                        if (data.error != 0)//错误
                        {
                            ajaxform.errorbefore();
                            layer.alert(data.message, {
                                icon: 2,
                                btn: false,
                                closeBtn: 0,
                                time: 2000
                            });

                        } else
                        {
                            ajaxform.oktips();
                        }

                    },
                    error: function (data, status, xhr, $form)
                    {

                    }

                });
    },

    oktips: function () {

        switch (ajaxform.oktipsoption.type)
        {
            case - 2://不跳转刷新
                window.location.reload();
                break;
            case - 1://只提示不跳转

                layer.msg(ajaxform.oktipsoption.msg, {
                    title: '信息',
                    icon: 1,
                    btn: false,
                    time: 1000,
                    closeBtn: 0
                });
                break;
            case 0://直接跳转
                if (ajaxform.oktipsoption.url == '')
                {
                    window.location.reload();
                } else
                {
                    window.location.href = ajaxform.oktipsoption.url;
                }

                break;
            case 1://提示后跳转
                layer.msg(ajaxform.oktipsoption.msg, {
                    title: '信息',
                    icon: 1,
                    btn: false,
                    time: 1000,
                    closeBtn: 0
                }, function () {

                    if (ajaxform.oktipsoption.url == '')
                    {
                        window.location.reload();
                    } else
                    {
                        window.location.href = ajaxform.oktipsoption.url;
                    }

                });

                break;


        }




    }
}

